package xyz.wpxy.wxpushmsg.controller;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.wpxy.wxpushmsg.common.R;
import xyz.wpxy.wxpushmsg.constant.WxMsgConstant;
import xyz.wpxy.wxpushmsg.entity.WxConfig;
import xyz.wpxy.wxpushmsg.service.WxPushMsgService;

/**
 * @Description: 微信推送信息
 * @author: Peng Wei
 * @date:2023/8/7 15:59
 */
@Slf4j
@RestController
@AllArgsConstructor
public class PushMsgController {

    public final WxConfig wxConfig;
    public final WxPushMsgService weChatMessageService;


    /**
     * 发送文本消息
     *
     * @return
     */
    @GetMapping("/sendMsg")
    public R sendText(@RequestParam(required = false) String content,
                      @RequestParam(required = false) String type) {
        //初始化token
        String token = weChatMessageService.getAccessToken();

        //后期可用前端配置用户信息 多个用户:
        String phone = "13023069582";
        String phone2 = "18175334560";

        //得到userId
        String userId = weChatMessageService.getUserId(token, phone);
        //获取userId2 :
        String userId1 = weChatMessageService.getUserId(token, phone2);

        //多人发送的组合userId（注:多人发送中间以'|'隔开）:
        String userIds = userId + "|" + userId1;

        //推送的消息内容(空值 赋默认值):
        if (StringUtils.isEmpty(content)) {
            content = "正在推送告警信息wp";
        }
        //构造消息体
        StringBuffer sb = new StringBuffer();

        switch (type) {
            //文本:
            case WxMsgConstant.TYPE_TEXT:
                sb.append("{");
                sb.append("\"touser\":" + "\"" + userIds + "\",");
                sb.append("\"msgtype\":" + "\"" + "text" + "\",");
                sb.append("\"agentid\":" + "\"" + wxConfig.getAgentId() + "\",");
                sb.append("\"text\":" + "{");
                sb.append("\"content\":" + "\"" + content + "\"},");
                sb.append("\"safe\":\"0\"");
                sb.append("}");
                break;
            //文本卡片：
            case WxMsgConstant.TYPE_TEXT_CARD:
                sb.append("{");
                sb.append("\"touser\":\"" + userIds + "\",");
                sb.append("\"msgtype\":\"textcard\",");
                sb.append("\"agentid\":\"" + wxConfig.getAgentId() + "\",");
                sb.append("\"textcard\" : {" +
                        "\"title\" : \"告警通知\"," +
                        "\"description\" : \"<div class='gray'>2023年8月7日</div> <div class='normal'>发动机温度过高触发告警</div><div class='highlight'>请及时到现场查看并检修</div>\"," +
                        "\"url\" : \"www.baidu.com\"," +
                        "\"btntxt\":\"详情\"" +
                        "}");
                sb.append("\"safe\":\"1\"");
                sb.append("}");
                break;
        }

        String jsonString = JSON.toJSONString(sb.toString());
        log.info("信息体:{}", jsonString);

        //发送消息
        String result = weChatMessageService.sendMessage(token, sb.toString());
        log.info("推送返回结果:{}", result);

        return R.ok().message(result);
    }
}
